
from ast import Await
import time
from asyncio.tasks import Task
from tkinter import E
from base import *
import _thread
from PIL import Image
from io import BytesIO

class Shimen(Base):
    ShiMenDetail=None
    PrePosition=None
    EventLingqu=Event('./pic/shimen/lingqu.png',RECTS.Task)
    EventShimenRenwu=Event('./pic/shimen/shimen_renwu.png',RECTS.Task)
    EventQuwancheng=Event('./pic/shimen/quwancheng.png',RECTS.BottomHalf)
    EventXuanze=Event('./pic/shimen/xuanze.png',RECTS.BottomHalf)
    EventShicha=Event('./pic/shimen/shicha.png',RECTS.RightHalf)
    EventXuanzeDaan=Event('./pic/shimen/xuanze_daan.png',RECTS.RightHalf) 
    EventShiMenDone=Event('./pic/shimen/done.png',RECTS.RightHalf) 
    EventXuanzeyaozuodeshi=Event('./pic/base/xuanzeyaozuodeshi.png',RECTS.RightHalf)
    EventWendaLab=Event('./pic/shimen/wenda.png',RECTS.RightHalf)
    
    EventWenDa={
        Event('./pic/shimen/wenda_qs_feiyouxibaoshi.png',RECTS.BottomHalf):Event('./pic/shimen/wenda_ans_feiyouxibaoshi.png',RECTS.RightHalf),
        Event('./pic/shimen/wenda_qs_dazao.png',RECTS.BottomHalf):Event('./pic/shimen/wenda_ans_dazao.png',RECTS.RightHalf),
        Event('./pic/shimen/wenda_qs_shimengeshu.png',RECTS.BottomHalf):Event('./pic/shimen/wenda_ans_shimen20.png',RECTS.RightHalf),
        Event('./pic/shimen/wenda_qs_chuandai.png',RECTS.BottomHalf):Event('./pic/shimen/wenda_ans_chuandai.png',RECTS.RightHalf),
        Event('./pic/shimen/wenda_qs_sanjie.png',RECTS.BottomHalf):Event('./pic/shimen/wenda_ans_sanjie.png',RECTS.RightHalf),
        Event('./pic/shimen/wenda_qs_huaguojineng.png',RECTS.BottomHalf):Event('./pic/shimen/wenda_ans_huaguojineng.png',RECTS.RightHalf)
        }

    def AutoFlush(self):
        wsd=adbutils.adb.device('127.0.0.1:7555')
        lport=wsd.forward_port(7912)
        ws=websocket.WebSocket()
        ws.connect("ws://localhost:{}/minicap".format(lport))
        while True:
            data=None
            data=ws.recv()
            if not isinstance(data,(bytes,bytearray)):
                continue
            imgtmp=Image.open(BytesIO(data))
            screen=cv2.cvtColor(np.array(imgtmp), cv2.COLOR_BGR2GRAY)
            size=screen.shape
            self.img = cv2.resize(screen,(int(size[1]*self._widthScale),int(size[0]*self._heightScale)),interpolation= cv2.INTER_LINEAR)



    def Run(self):
        #_thread.start_new_thread(self.AutoFlush)
        # while True:
        #     self.showImage(self.Image())
        self.UseImg()
        if self.find(self.EventLingqu):
            self.Lingqu(self.EventLingqu.Position())
        while True: 
            self.screenShot()
            #self.waitStop()
            self.UseImg()
            # self.showImage(self.Image())
            #start_time = time.time()
            if self.BaseRun():
                pass
            elif self.find(self.EventXuanzeyaozuodeshi):
                print('选择要做的事')
                if self.find(self.EventShicha):
                    print('选择师门相关任务')
                    self.click(*self.EventShicha.Position())
                else:
                    print('未找到师门相关选项，选择第一个')
                    pos=self.EventXuanzeyaozuodeshi.Position()
                    self.click(pos[0]+154,pos[1]+92)
                time.sleep(0.5)
            elif self.find(self.EventZidong) or self.find(self.EventGuajiQuxiao):
                self.waitFight()
                continue
            elif self.find(self.EventWendaLab):
                self.Wenda()
            elif self.find(self.EventShimenRenwu):
                if self.PrePosition is None:
                    print('点击任务栏师门任务')
                    self.click(*self.EventShimenRenwu.Position())
                else:
                    if self.isCmpare(self.PrePosition,self.Image()[60:80,149:202]):
                        print('点击任务栏师门任务')
                        self.click(*self.EventShimenRenwu.Position())
                self.PrePosition=self.Image()[60:80,149:202]
                time.sleep(0.5)
            elif self.find(self.EventShiMenDone):
                print('师门任务完成')
                x,y=self.EventShiMenDone.Position()
                self.click(x+35,y+30)
                self.showImage(self.Image())
                break
            elif self.find(self.EventZhiyin) and not self.find(self.EventShimenRenwu) and not self.find(self.EventXuanzeyaozuodeshi):
                print('师门任务完成')
                self.showImage(self.Image())
                break
            #self.waitFlush()
            #end_time = time.time()
            #print("耗时: {:.2f}秒".format(end_time - start_time))

    def Lingqu(self,pos):
            #点击任务栏师门任务
        print('领取师门任务')
        self.click(pos[0],pos[1])
        time.sleep(1)
        self.screenShot()
        self.UseImg()
        if self.find(self.EventQuwancheng):
            self.click(*self.EventQuwancheng.Position())
        elif self.find(self.EventXuanze):
            self.click(*self.EventXuanze.Position())
        time.sleep(1)

    def Wenda(self):
        print('问答任务')
        for key,value in self.EventWenDa.items():
            if self.find(key) and self.find(value):
                self.click(*value.Position())
                return
        print('未找到答案')

    

shimen=Shimen()
shimen.Run()